// 固定初始路由
import { constantRoutes } from './router.constant'
import store from '@/store'

// 各应用路由
let appRouter = []
try {
  // 获取同级目录modules下所有的js文件模块
  const context = require.context('./modules', true, /\/.*\.js$/)
  // 遍历相对路径获取对应路径下的模块，进行应用部分的路由组装
  context.keys().forEach((key) => {
    const appConf = context(key).default
    if (appConf instanceof Array) {
      appRouter = [...appRouter, ...appConf]
    } else {
      appRouter.push(appConf)
    }
  })
} catch (e) {
  throw new Error('应用路由获取错误：' + e)
}

// 初始化路由信息
store.dispatch('initRouters', appRouter)

// 合并初始路由和应用路由
const routes = [...constantRoutes, ...appRouter]

export { routes }
